using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace ScreenMonkey.Plugin.Clips.Song
{
    public static class VerseLabels
    {
        public static event EventHandler LabelAdded;
        public static event EventHandler LabelRemoved;
        public static event EventHandler LabelNameChanged;

        private static List<VerseLabel> labelsList = new List<VerseLabel>();

        public static List<VerseLabel> Labels
        {
            get {
                return labelsList; 
            }
        }

        private static bool defaultsLoaded = false;

        public static void LoadDefaults(bool notify)
        {
            defaultsLoaded = true;

            labelsList.Clear();

            AddLabel(new VerseLabel("Plain Text", "P"));                      //Add the default label
            AddLabel(new VerseLabel("Chorus", "C"));
            AddLabel(new VerseLabel("Bridge", "B"));
            AddLabel(new VerseLabel("Verse 1", "1"));
            AddLabel(new VerseLabel("Verse 2", "2"));
            AddLabel(new VerseLabel("Verse 3", "3"));
            AddLabel(new VerseLabel("Verse 4", "4"));
            AddLabel(new VerseLabel("Verse 5", "5"));
            AddLabel(new VerseLabel("Verse 6", "6"));
            AddLabel(new VerseLabel("Verse 7", "7"));
            AddLabel(new VerseLabel("Verse 8", "8"));
            AddLabel(new VerseLabel("Verse 9", "9"));

            if (LabelAdded != null && notify) LabelAdded(null, EventArgs.Empty);
        }

        private static void AddLabel(VerseLabel label)
        {
            if (FindLabel(label.Id) != null) 
                throw new ArgumentException("A label with the same ID already exists. The Id must be unique.");

            label.Strict = true;
            label.GlobalId = Guid.NewGuid();
            label.NameChanged += LabelNameChanged;
            labelsList.Add(label);
        }

        public static void Add(VerseLabel label)
        {
            AddLabel(label);
            if (LabelAdded != null) LabelAdded(null, EventArgs.Empty);
        }

        public static void Delete(VerseLabel label)
        {
            labelsList.Remove(label);

            if (LabelRemoved != null) LabelRemoved(null, EventArgs.Empty);
        }

        public static VerseLabel FindQuickName(string name)
        {
            if (!string.IsNullOrEmpty(name))
            {
                foreach (VerseLabel item in labelsList)
                {
                    if (item.QuickName.ToUpper() == name.ToUpper()) return item;
                }
            }

            return null;
        }

        public static VerseLabel FindLabel(string id)
        {
            foreach (VerseLabel item in Labels)
            {
                if (item.Id == id) return item;
            }

            return null;
        }

        public static void SaveXml(XmlWriter writer)
        {
            writer.WriteStartElement("tags");

            foreach (VerseLabel label in Labels)
            {
                label.SaveXml(writer);
            }

            writer.WriteEndElement();
        }

        public static void LoadXml(XmlNode xml)
        {
            if (!defaultsLoaded) LoadDefaults(false);

            foreach (XmlNode tagXml in xml.SelectNodes("tags/tag"))
            {
                VerseLabel label = new VerseLabel(tagXml);
                VerseLabel existingLabel = FindLabel(label.Id);   
                    
                if(existingLabel == null)
                {
                    //This label does not exist so add it.
                    AddLabel(label);
                }
                else
                {
                    //This label already exists so load the new settings into it.
                    existingLabel.LoadXml(tagXml);
                }                
            }

            //Notify clients that the labels have changed.
            if (LabelAdded != null) LabelAdded(null, EventArgs.Empty);
        }
		
    }
}
